<extend name="Layout/ins_page" />
<block name="content">
	<style>
		.sub-sidebar {
			margin-left: 0px;
			margin-right: 0px;
		}
		.sub-sidebar li.active {
			background: #f2f2f2;
		}
		.content-wrap {
			padding-left: 7px;
			padding-right: 7px;
			padding-top: 3px;
			padding-bottom: 3px;
		}
		.content-wrap * {
			line-height: 28px;
		}
		.html {
			height: 100%;
		}

		@media screen and (max-width: 768px) {
			.layui-form-label {
				width: 50px;
			}
			.layui-input-block {
				margin-left: 55px;
			}
			.tt .layui-form-label {
				width: 100%;
				text-align: left;
				position: static;
				float: none;
			}
			.tt .layui-input-block {
				width: 100%;
				text-align: left;
				margin-left: 0px;
				position: static;
			}
			.sub-content {
				margin-left: 0px;
				margin-right: 0px;
			}
		}
	</style>
	{:W('PageHeader/adv_search',array('name'=>$work_title.'日报','search'=>'M'))}
	<div class="x-panel hidden" id="adv_search">
		<form method="post" name="form_adv_search" id="form_adv_search" >
			<div class="x-panel-header">
				<h4>高级搜索</h4>
				<div class="split"></div>
				<a  class="btn" id="submit_adv_search">搜索</a>
				<a  class="btn btn-cancel" id="close_adv_search">关闭</a>
			</div>
			<div class="x-panel-header-body">
				<div class="frame">
					<div class="bit-2">
						<div class="layui-form-item">
							<label class="layui-form-label" for="eq_dept_id">部门：</label>
							<div class="layui-input-block">
								<select class="layui-input" name="eq_dept_id">
									<option value="">选择部门</option>
									{$dept_list}
								</select>
							</div>
						</div>
					</div>
					<div class="bit-2">
						<div class="layui-form-item">
							<label class="layui-form-label" for="eq_user_id">登录人：</label>
							<div class="layui-input-block">
								<input class="layui-input" type="text" id="eq_user_id" name="eq_user_id">
							</div>
						</div>
					</div>
				</div>
				<div class="frame">
					<div class="bit-2">
						<div class="layui-form-item">
							<label class="layui-form-label" >内容：</label>
							<div class="layui-input-block">
								<input class="layui-input" type="text" id="li_content" name="li_content">
							</div>
						</div>
					</div>
					<div class="bit-2">
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">登录时间：</label>
								<div class="layui-input-inline" style="width: 100px;">
									<input type="text" class="layui-input input-start-date" name="be_create_time" readonly="readonly" />
								</div>
								<div class="layui-form-mid">
									-
								</div>
								<div class="layui-input-inline" style="width: 100px;">
									<input type="text" class="layui-input input-end-date" name="en_create_time" readonly="readonly" />
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</form>
	</div>
	<div class="oprator-panel">
		<span class="split"></span>
		<a class="btn" onclick="add()">新建</a>
	</div>
	<div class="layui-tab layui-tab-brief">
		<ul class="layui-tab-title">
			<li>
				<a class="link" href="{:U('index')}">我的日报</a>
			</li>
			<li>
				<a class="link" href="{:U('sub')}">下属的日报</a>
			</li>
			<li>
				<a class="link layui-this" href="{:U('comment_me')}">评论我的</a>
			</li>
			<li>
				<a class="link" href="{:U('reply_me')}">回复我的</a>
			</li>
		</ul>
	</div>
	<div class="sub-frame">
		<div class="sub-sidebar">
			<div class=" ul-table">
				<ul>
					<li class="thead">
						<span class="col-8 grow-1">部门</span>
						<span class="col-5">姓名</span>
						<span class="col-8">日期</span>
						<span class="col-4">操作</span>
					</li>
					<volist name="list" id="vo">
						<li data="{$vo.id}">
							<span class="col-8  grow-1">{$vo.dept_name}</span>
							<span class="col-5">{$vo.user_name}</span>
							<span class="col-8">{$vo.work_date}</span>
							<span class="col-4">
								<if condition="$vo.user_id eq get_user_id()">
									<a href="{:U('edit',array('id'=>$vo['id']))}">修改</a>
								</if></span>
						</li>
					</volist>
				</ul>
			</div>
			<notempty name="list">
				<div class="pagination">
					{$page}
				</div>
			</notempty>
		</div>
		<div class="sub-content">
			<div class="frame" >
				<div class="bit-1" style="background:#f5f5f5;padding-top:0px;border:1px solid #ddd;">
					<div class="layui-form-item">
						<label class="layui-form-label">标题：</label>
						<div class="layui-input-block">
							<div class="layui-input-static">
								<span id="name"></span>
							</div>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">日期：</label>
						<div class="layui-input-block">
							<div class="layui-input-static">
								<span id="work_date"></span>
							</div>
						</div>
					</div>
					<div class="layui-form-item tt">
						<label class="layui-form-label">内容：</label>
						<div class="layui-input-block">
							<div class="content-wrap" id="work_log_content" style="height:200px;overflow-y: auto;"></div>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">附件：</label>
						<div class="layui-input-block" id="add_file">
							<ul class="file_list" id="file_list"></ul>
						</div>
					</div>
				</div>
				<div id="good_panel" class="bit-1 hidden" style="background:#f5f5f5;border:1px solid #ddd;margin-top:20px;line-height:40px;">
					<i class="fa fa-heart-o" aria-hidden="true"></i>
					<span id="good" data=""></span>
					<span class="split"></span>
					<span style="float: right;"><a id="btn_good">点赞</a>&nbsp;<a id="toggle_comment">评论</a> </span>
				</div>
				<div id="comment_box" class="bit-1 hidden" style="background:#f5f5f5;padding-top:15px;border:1px solid #ddd;margin-top:20px;">
					<form method='post' id="form_comment" name="form_comment" enctype="multipart/form-data" >
						<div class="layui-form-item tt">
							<label class="layui-form-label">评论：</label>
							<div class="layui-input-block" >
								<textarea class="layui-textarea simple" style="height:110px;" id="content" name="content"></textarea>
							</div>
						</div>
						<div class="layui-form-item hidden">
							<label class="layui-form-label" for="name">附件：</label>
							<div class="layui-input-block">
								{:W('FileUpload/add')}
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label"></label>
							<div class="layui-input-block" >
								<a class="btn" id="btn_comment" data="comment">提交</a>
							</div>
						</div>
					</form>
				</div>
				<div id="comment_content"></div>
			</div>
		</div>
	</div>
	<div class="hidden" id="tpl_comment">
		<div class="bit-1" style="background:#f5f5f5;padding-top:0px;border:1px solid #ddd;margin-top:20px;padding-left:5px;" user_id="{user_id}" user_name="{user_name}">
			<label class="layui-form-label" >{user_name}</label>
			<div class="layui-input-block" >
				<div class="layui-input-static">
					{create_time}{btn}
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block" >
					<div class="content-wrap">
						{content}
					</div>
				</div>
			</div>
			<div class="reply-box"></div>
		</div>
	</div>
	<div id="reply_box" class="hidden">
		<div class="layui-form-item">
			<label class="layui-form-label">回复：</label>
			<div class="layui-input-block dd" style="height:110px;">

			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-block" >
				<a class="btn btn-save-reply">提交</a>
			</div>
		</div>
	</div>
</block>
<block name="js">
	<script type="text/javascript">
		layui.use(['global', 'editor'], function() {
			var $ = layui.jquery;
			set_return_url(null);
			var fn = {
				add : function() {
					window.open("{:U('add')}", "_self");
				}
			};
			win_exp(fn);
			//查看内容
			$(".sub-sidebar li").on("click", function() {
				$(".sub-sidebar li").removeClass('active');
				$("#good_panel").removeClass('hidden');
				$(this).addClass("active");
				$id = $(this).attr("data");
				var user_id = '{:get_user_id()}';
				send_ajax("{:U('get_work_log')}", "id=" + $id, function(ret) {
					$("#create_time").text(to_date(ret.create_time));
					$("#work_log_user").text(ret.user_name);
					$("#work_date").text(ret.work_date);
					$("#work_log_content").html(ret.content);
					$("#name").html(ret.name);
					$("#file_list").html(ret.html);

					var good = '';
					var status = 0;
					json = JSON.parse(ret.good);
					for (k in json ) {
						good += json[k];
						if (user_id == k) {
							status = 1;
						}
					}
					$('#good').text(good);
					if (status) {
						$('#btn_good').text('取消');
					} else {
						$('#btn_good').text('点赞');
					}
					$("#comment_content").html('');
					render_comment(ret);
					if ( typeof (tinyMCE) != 'undefined') {
						tinyMCE.triggerSave(true);
					}
				});
			});

			//点赞
			$("#btn_good").on('click', function() {
				send_ajax("{:U('good')}", "id=" + $id, function(ret) {
					var good = '';
					for (k in ret.data) {
						good += ret.data[k];
					}
					$('#good').text(good);
					if (ret.status) {
						$('#btn_good').text('取消');
					} else {
						$('#btn_good').text('点赞');
					}
				});
			});

			//展开评论
			$("#toggle_comment").on('click', function() {
				$("#content").text('222222');
				tinymce.get('content').load();
				if ($('#comment_box').hasClass('hidden')) {
					$('#comment_box').removeClass('hidden');
				} else {
					$('#comment_box').addClass('hidden');
				}
			});

			//提交评论
			$("#btn_comment").on('click', function() {
				if ( typeof (tinyMCE) != 'undefined') {
					tinyMCE.triggerSave(true);
				}
				var vars = $("#form_comment").serialize();
				send_ajax("{:U('comment')}", vars + "&row_id=" + $id, function(ret) {
					if (ret.stauts == 1) {
						layer.msg(ret.info, {
							time : 1200
						}, function() {
							$("#content").val('');
							tinymce.get('content').load();
							$("#comment_box").addClass('hidden');
							render_comment(ret);
						});
					} else {
						layer.msg(ret.info);
					}
				});
			});

			//展开回复
			$(document).on('click touchstart', '.btn-reply', function() {
				$this = $(this);
				$parent = $this.parents('div.bit-1');
				$reply = $parent.find('.reply-box');
				open = $this.attr('open');
				if (open == null) {
					open = $this.attr('open', true);
					$html = $("#reply_box").html();
					$reply.html($html);
					var el = document.createElement('textarea');
					el.value = "回复【" + $parent.attr('user_name') + "】: ";
					$parent.find('.dd').append(el);
					tinymce.init({
						target : el,
						menu : {},
						language : "zh_CN",
						plugins : [],
						height : 110,
						toolbar : false,
						statusbar : false,
						contextmenu : false
					});
				} else {
					$reply.html('');
					open = $this.attr('open', null);
				}
			});

			//提交回复
			$(document).on('click touchstart', '.btn-save-reply', function() {
				$this = $(this);
				$parent = $this.parents('div.bit-1');
				reply_to_user_id = $parent.attr('user_id');
				reply_to_user_name = $parent.attr('user_name');
				if ( typeof (tinyMCE) != 'undefined') {
					tinyMCE.triggerSave(true);
				}
				content = $parent.find('textarea').val();

				vars = 'reply_to_user_id=' + reply_to_user_id;
				vars += '&reply_to_user_name=' + reply_to_user_name;
				vars += '&content=' + content;
				vars += '&row_id=' + $id;
				send_ajax("{:U('reply')}", vars, function(ret) {
					if (ret.stauts == 1) {
						layer.msg(ret.info, {
							time : 1200
						}, function() {
							$reply.html('');
							open = $this.attr('open', null);
							render_comment(ret);
						});
					} else {
						layer.msg(ret.info);
					}
				});
			});

			//删除
			$(document).on('click touchstart', '.btn-del-reply', function() {
				$this = $(this);
				$parent = $this.parents('div.bit-1');
				layer.msg('确定要删除吗?', {
					time : 0,
					btn : ['确定', '取消'],
					yes : function(index) {
						var vars = "comment_id=" + $this.attr('data');
						send_ajax("{:U('del_comment')}", vars, function(ret) {
							if (ret.status == 1) {
								layer.close(index);
								$parent.remove();
							}
						});
					},
					no : function(index) {
						layer.close(index);
					}
				});

			});

			function to_date(date) {
				var date = new Date(parseInt(date) * 1000);
				var seperator1 = "-";
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var strDate = date.getDate();
				var hour = date.getHours();
				var min = date.getMinutes();
				if (month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if (strDate >= 0 && strDate <= 9) {
					strDate = "0" + strDate;
				}
				if (min >= 0 && min <= 9) {
					min = "0" + min;
				}
				var currentdate = year + seperator1 + month + seperator1 + strDate + ' ' + hour + ":" + min;
				return currentdate;
			};

			function render_comment(ret) {
				var user_id = '{:get_user_id()}';
				$("#comment_content").html('');
				for (k in ret.comment ) {
					vo = ret.comment[k];
					var tpl_comment = $("#tpl_comment").html();
					tpl_comment = tpl_parse(tpl_comment, 'user_id', vo.user_id);
					tpl_comment = tpl_parse(tpl_comment, 'user_name', vo.user_name);
					tpl_comment = tpl_parse(tpl_comment, 'create_time', to_date(vo.create_time));
					tpl_comment = tpl_parse(tpl_comment, 'content', vo.content);
					if (user_id == vo.user_id) {
						btn = '<a class="btn-del-reply" data=' + vo.id + ' style="float:right">删除</a>';
						tpl_comment = tpl_parse(tpl_comment, 'btn', btn);
					} else {
						btn = '<a class="btn-reply" data=' + vo.id + ' style="float:right">回复</a>';
						tpl_comment = tpl_parse(tpl_comment, 'btn', btn);
					}
					$("#comment_content").append(tpl_comment);
				}
			}

		});
	</script>
</block>